package day20170424.homework;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.junit.Test;

public class HomeWork01 {
	/**
	 * 1: 使用字节流  把 io1704下的aa.txt中的内容复制到bb.txt  如果是中文会有乱码问题
	 * @throws Exception
	 */
	@Test
	public void homework01(){
		//定义输入流
		InputStream is = null;
		//定义输出流
		OutputStream os = null;
		try {
			//读入的路径
			File src = new File("txt/aa.txt");
			//写出的路径
			File dest = new File("txt/bb.txt");
			is = new FileInputStream(src);
			os = new FileOutputStream(dest);
			//每次读取多少个字节
			byte[] bs = new byte[3];
			int len;
			//长度不等于-1，继续读取
			while((len = is.read(bs)) != -1){
				//写入新的文件
				os.write(bs, 0, len);
				os.flush();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(null != is){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(null != os){
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
